package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.PayeeInfoBean;
import cc.rengu.igas.channel.upqc.facade.request.GenQrCodeRequest;
import cc.rengu.igas.channel.upqc.facade.response.GenQrCodeResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;


/**
 * 生成二维码
 */
public class GenQrCodeService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        GenQrCodeRequest genQrCodeRequest = new GenQrCodeRequest();
        ConvertUtil.convertOutput(genQrCodeRequest);
        return genQrCodeRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        GenQrCodeResponse genQrCodeResponse = new GenQrCodeResponse();
        GenQrCodeRequest genQrCodeRequest = (GenQrCodeRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(genQrCodeRequest);
        packagePayeeInfo(genQrCodeRequest.getPayeeInfoBean());
        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, genQrCodeRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, genQrCodeRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            genQrCodeResponse.setRespCode("04");
            genQrCodeResponse.setRespDesc("交易状态未明");
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维码服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(genQrCodeResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(genQrCodeResponse.getRespCode())) {
            genQrCodeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            genQrCodeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(genQrCodeResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        GenQrCodeResponse genQrCodeResponse = (GenQrCodeResponse) response.getResult();
        ConvertUtil.convertInput(genQrCodeResponse);
    }

    private void packagePayeeInfo(PayeeInfoBean payeeInfoBean) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (payeeInfoBean != null && isNotEmpty(payeeInfoBean)) {
            StringBuffer payeeInfo = new StringBuffer(UpqcParamConstant.LEFT_BRACE);
            //StringBuffer payeeInfo = new StringBuffer();
            if (null != payeeInfoBean.getName() && !payeeInfoBean.getName().isEmpty()) {
                payeeInfo.append("name" + UpqcParamConstant.EQUAL + payeeInfoBean.getName() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payeeInfoBean.getId() && !payeeInfoBean.getId().isEmpty()) {
                payeeInfo.append("id" + UpqcParamConstant.EQUAL + payeeInfoBean.getId() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payeeInfoBean.getMerCatCode() && !payeeInfoBean.getMerCatCode().isEmpty()) {
                payeeInfo.append("merCatCode" + UpqcParamConstant.EQUAL + payeeInfoBean.getMerCatCode() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payeeInfoBean.getTermId() && !payeeInfoBean.getTermId().isEmpty()) {
                payeeInfo.append("termId" + UpqcParamConstant.EQUAL + payeeInfoBean.getTermId() + UpqcParamConstant.AMPERSAND);
            }
            if (null != payeeInfoBean.getAccNo() && !payeeInfoBean.getAccNo().isEmpty()) {
                payeeInfo.append("accNo" + UpqcParamConstant.EQUAL + payeeInfoBean.getAccNo() + UpqcParamConstant.AMPERSAND);
            }
            payeeInfo.setLength(payeeInfo.length() - 1);
            payeeInfo.append(UpqcParamConstant.RIGHT_BRACE);
            xmlTreeUtil.setXmlTreeStringValue("payeeInfo", encData(payeeInfo.toString()));
        }
    }
}
